package com.ikang.myapplication;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private CardView cardView;
    private RippleBackground rippleBackground;
    private Button btnStart, btnStop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
        cardView = findViewById(R.id.cardView);
        rippleBackground = findViewById(R.id.rippleBackground);
        btnStart = findViewById(R.id.btnStart);
        btnStop = findViewById(R.id.btnStop);

        // 设置按钮点击事件
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 如果动画没有运行，则开始动画
                if (!rippleBackground.isRippleAnimationRunning()) {
                    rippleBackground.startRippleAnimation();
                    btnStart.setText("暂停动画");
                } else {
                    // 如果动画正在运行，则暂停动画
                    rippleBackground.pauseRippleAnimation();
                    btnStart.setText("继续动画");
                }
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 停止动画
                rippleBackground.stopRippleAnimation();
                btnStart.setText("开始动画");
            }
        });

        // 启动呼吸动画
        startBreathingAnimation();
    }

    private void startBreathingAnimation() {
        // 将动画作用对象改为imageView，从原图开始缩小再恢复
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.85f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 0.85f, 1f);
        
        // 匹配扩散动画的时长
        scaleX.setDuration(1500);
        scaleY.setDuration(1500);
        scaleX.setRepeatCount(ValueAnimator.INFINITE);
        scaleY.setRepeatCount(ValueAnimator.INFINITE);
        
        // 使用 AccelerateDecelerateInterpolator 使动画更平滑
        scaleX.setInterpolator(new AccelerateDecelerateInterpolator());
        scaleY.setInterpolator(new AccelerateDecelerateInterpolator());
        
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(scaleX, scaleY);
        animatorSet.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (rippleBackground != null) {
            rippleBackground.stopRippleAnimation();
        }
    }
}